package gui;

import java.rmi.RemoteException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import server.AdminServer;
import server.PlayerServer;
import server.Server;
import server.ServerTester;

public class MainWindow extends JApplet {

	private Server viewServer;
	private PlayerServer playerServer;
	private AdminServer adminServer;
	
	private UserType user;
	private Box mainBox;
	private WorldViewPanel worldView;
	private ControlPanel controls;
	private InformationPanel info;
	private JScrollPane worldPane;
	private FileBarPanel fileBar;
	
	public MainWindow(){
//		try {
//			Registry registry = LocateRegistry.getRegistry(null);
//			viewServer = (Server) registry.lookup("server");
//			worldView = new WorldViewPanel(viewServer);
//		} catch (RemoteException e) {
//			JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(this),
//					e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
//			try {
//				Thread.sleep(3000);
//			} catch (InterruptedException e1) {}
//			System.exit(0);
//		} catch (NotBoundException e) {
//			JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(this),
//					e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
//			try {
//				Thread.sleep(3000);
//			} catch (InterruptedException e1) {}
//			System.exit(0);
//		}
		
		viewServer = ServerTester.getServer();
		try {
			worldView = new WorldViewPanel(viewServer);
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(this),
			e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
		}
		
		info = new InformationPanel(viewServer, worldView);
		mainBox = new Box(BoxLayout.Y_AXIS);
		add(mainBox);
		user = UserType.ADMIN;
	}
	
	public void init(){
		setSize(1000, 600);
		changeUser(viewServer, user);
		setVisible(true);
	}

	//called with newServer == null for logging out
	public void changeUser(Server newServer, UserType userType) {
		user = userType;
		mainBox.removeAll();
		worldPane = new JScrollPane(worldView);
		if (user == UserType.ADMIN) {
			adminServer = (AdminServer) newServer;
			fileBar = new FileBarPanel(user, adminServer, this, worldView);
			Box simulationBox = new Box(BoxLayout.X_AXIS);
			Box worldBox = new Box(BoxLayout.Y_AXIS);
			mainBox.add(fileBar);
			mainBox.add(simulationBox);
			controls = new ControlPanel(adminServer, worldView);
			simulationBox.add(worldBox);
			simulationBox.add(controls);
			worldBox.add(worldPane);
			worldBox.add(info);
		} else if(user == UserType.USER_ADMIN){
			adminServer = (AdminServer) newServer;
			fileBar = new FileBarPanel(user, adminServer, this, worldView);
			mainBox.add(fileBar);
			mainBox.add(worldPane);
			mainBox.add(info);
		} else if(user == UserType.USER){
			playerServer = (PlayerServer) newServer;
			fileBar = new FileBarPanel(user, playerServer, this, worldView);
			mainBox.add(fileBar);
			mainBox.add(worldPane);
			mainBox.add(info);			
		} else{
			adminServer = null;
			playerServer = null;
			fileBar = new FileBarPanel(user, viewServer, this, worldView);
			mainBox.add(fileBar);
			mainBox.add(worldPane);
			mainBox.add(info);		
		}
		validate();
	}
	
	public void switchAdminView(){
		if(user == UserType.ADMIN)
			user = UserType.USER_ADMIN;
		else if (user == UserType.USER_ADMIN)
			user = UserType.ADMIN;
		else
			throw new UnsupportedOperationException("Can only switch views if logged in as an admin");
		
		mainBox.removeAll();
		worldPane = new JScrollPane(worldView);
		if (user == UserType.ADMIN) {
			fileBar = new FileBarPanel(user, adminServer, this, worldView);
			Box simulationBox = new Box(BoxLayout.X_AXIS);
			Box worldBox = new Box(BoxLayout.Y_AXIS);
			mainBox.add(fileBar);
			mainBox.add(simulationBox);
			controls = new ControlPanel(adminServer, worldView);
			simulationBox.add(worldBox);
			simulationBox.add(controls);
			worldBox.add(worldPane);
			worldBox.add(info);
		} else {
			fileBar = new FileBarPanel(user, adminServer, this, worldView);
			mainBox.add(fileBar);
			mainBox.add(worldPane);
			mainBox.add(info);
		}
		validate();
	}
}
